/*
 * Copyright 2011 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.gaellalire.jzip.macosx.cocoa;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JOptionPane;

import org.rococoa.ID;
import org.rococoa.Rococoa;
import org.rococoa.cocoa.foundation.NSArray;
import org.rococoa.cocoa.foundation.NSObject;
import org.rococoa.cocoa.foundation.NSString;
import org.rococoa.cocoa.foundation.NSUInteger;

import fr.gaellalire.jzip.view.JZipActivitiesFrame;

/**
 *
 * @author Gael Lalire
 */
public class ServicesProvider {

    private NSObject proxy;

    private ID id;

    private Executor executor = Executors.newCachedThreadPool();

    public void serviceCompressFileUrl_(final NSPasteboard pboard, final String userData) {
        if (pboard.availableTypeFromArray(NSArray.CLASS.arrayWithObjects(NSString.stringWithString(NSPasteboard.FilenamesPboardType))) != null) {
            NSObject o = pboard.propertyListForType(NSPasteboard.FilenamesPboardType);
            if (o != null) {
                final NSArray elements = Rococoa.cast(o, NSArray.class);
                final List<File> files = new ArrayList<File>();
                for (int i = 0; i < elements.count(); i++) {
                    files.add(new File(elements.objectAtIndex(new NSUInteger(i).intValue()).toString()));
                }
                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        JZipActivitiesFrame.compress(files);
                    }
                });
            }
        }
    }

    public void serviceExtractFileUrl_(final NSPasteboard pboard, final String userData) {
        if (pboard.availableTypeFromArray(NSArray.CLASS.arrayWithObjects(NSString.stringWithString(NSPasteboard.FilenamesPboardType))) != null) {
            NSObject o = pboard.propertyListForType(NSPasteboard.FilenamesPboardType);
            if (o != null) {
                final NSArray elements = Rococoa.cast(o, NSArray.class);
                final List<File> files = new ArrayList<File>();
                for (int i = 0; i < elements.count(); i++) {
                    files.add(new File(elements.objectAtIndex(new NSUInteger(i).intValue()).toString()));
                }
                executor.execute(new Runnable() {

                    @Override
                    public void run() {
                        JOptionPane.showMessageDialog(null, "Service uncompress " + files);
                    }
                });
            }
        }
    }

    protected NSObject proxy(final Class<? extends NSObject> type) {
        if (null == proxy) {
            proxy = Rococoa.proxy(this, type);
        }
        return proxy;
    }

    protected org.rococoa.ID id(final Class<? extends NSObject> type) {
        if (null == id) {
            id = this.proxy(type).id();
        }
        return id;
    }

    public org.rococoa.ID id() {
        return this.id(NSObject.class);
    }

}